package app.client.collect.cartoon.web.sohu;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import app.client.collect.cartoon.BaseCollectAnalyse;
import app.client.collect.cartoon.entity.CartoonCollectEntity;
import cn.lily.com.file.io.FileInputManager;
import cn.lily.com.lang.Basic;

/**
 * SOHU 初步处理类
 * @author JiaJ
 * @date 2018年1月8日
 *
 */
public class SohuAnalyse extends BaseCollectAnalyse {
	
	public void analyseFile(File file){
		
		List<CartoonCollectEntity> entitys = new ArrayList<CartoonCollectEntity>();
		
		FileInputManager fileManager = new FileInputManager();
		
		fileManager.loadFile(file);
		
		try {
			
			String line1 = fileManager.readLine();
			
			while(line1 != null){
				
				CartoonCollectEntity entity = new CartoonCollectEntity();
				
				String line2 = fileManager.readLine();
				String line3 = fileManager.readLine();
				String line4 = fileManager.readLine();
								
				//第1行解析 - 数量
				if(line1.startsWith("共")){
					int st = line1.indexOf("共");
					int ed = line1.indexOf("集");				
					int num = Basic.getInt(line1.substring(st+1,ed));					
					entity.setCartoonNum(num);		
					entity.setStatus("完结");
				}
				else if(line1.startsWith("更新至")){
					int st = line1.indexOf("更新至");
					int ed = line1.indexOf("集");				
					int num = Basic.getInt(line1.substring(st+3,ed));					
					entity.setCartoonNum(num);	
					entity.setStatus("连载");
				}
				else{
					entity.setCartoonNum(0);
					entity.setStatus("未知");
					System.out.println("异常Line1:"+line1);
				}
				
				//第2行解析 - 名称
				if(line2 == null){
					line1 = line2;
					continue;
				}
				
				//名称与季数添加
				if(line2.indexOf("第") > 0){
					
					int st = line2.indexOf("第");
					int ed1 = line2.indexOf("季");
					int ed2 = line2.indexOf("部");
					
					String season = "";
					
					if(ed1 > 0){
						season = line2.substring(st+1,ed1);
					}
					else if(ed2 > 0){
						season = line2.substring(st+1,ed2);
					}
					
					entity.setSeason(season);					
					entity.setCartoonName(line2.substring(0,st));
					
				}
				else if(line2.endsWith("季") || line2.endsWith("部")){
					
					line2 = line2.substring(0,line2.length()-1);
					
					int edNum = Basic.getInt(line2.substring(line2.length()-1));
					
					if(edNum == 0){
						entity.setSeason("1");
						entity.setCartoonName(line2.trim());
					}else{
						
						int edNum2 = Basic.getInt(line2.substring(line2.length()-2));
						
						if(edNum2 == 0){
							entity.setSeason(edNum+"");
							entity.setCartoonName(line2.substring(0,line2.length()-1));
						}else{
							entity.setSeason(edNum2+"");
							entity.setCartoonName(line2.substring(0,line2.length()-2));
						}
						
					}
					
				}
				
				else{
					
					int edNum = Basic.getInt(line2.substring(line2.length()-1));
					
					if(edNum == 0){
						entity.setSeason("1");
						entity.setCartoonName(line2.trim());
					}else{
						
						int edNum2 = Basic.getInt(line2.substring(line2.length()-2));
						
						if(edNum2 == 0){
							entity.setSeason(edNum+"");
							entity.setCartoonName(line2.substring(0,line2.length()-1));
						}else{
							entity.setSeason(edNum2+"");
							entity.setCartoonName(line2.substring(0,line2.length()-2));
						}
						
					}
					

				}
				
				entitys.add(entity);
				
				//第3行：地区
				if(line3 == null || !line3.startsWith("地区")){
					line1 = line3;
					continue;
				}
				
				entity.setArea(line3.substring(3).trim());				
					
				//第4行不存在
				if(line4==null || !line4.startsWith("发布时间")){
					line1 = line4;
					continue;
				}
				
				String fileName = file.getName();
				
				String year =  fileName.substring(0,fileName.indexOf("."));
				
				//第4行 - 日期
				entity.setYear(year);
				
				line1 = fileManager.readLine();
				
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			fileManager.free();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String fileName = file.getName();
		
		int pointIdx =  fileName.indexOf(".");
		
		this.export(fileName.substring(0,pointIdx), entitys);
		
	}
	
	public void analyse(String folderPath) throws Exception{
		
		this.createWriter();
		
		File file = new File(folderPath);
		
		File[] files = file.listFiles();
		
		for(File active:files){
			this.analyseFile(active);
		}
		
		this.save();
		
		this.free();
	
	}
	
	public static void main(String[] args) throws Exception {
		
		SohuAnalyse analyse = new SohuAnalyse();
		
		analyse.analyse("E:/Analyse/搜狐UTF8");
		
	}
	
}
